package com.ruoyi.zsyh.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.zsyh.domain.ZsyhServiceOrder;
import com.ruoyi.zsyh.service.IZsyhServiceOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhServiceOrderLog;
import com.ruoyi.zsyh.service.IZsyhServiceOrderLogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单过程追踪Controller
 * 
 * @author yuexl
 * @date 2022-09-06
 */
@Controller
@RequestMapping("/zsyh/serviceOrderLog")
public class ZsyhServiceOrderLogController extends BaseController
{
    private String prefix = "zsyh/serviceOrderLog";

    @Autowired
    private IZsyhServiceOrderLogService zsyhServiceOrderLogService;
    @Autowired
    private IZsyhServiceOrderService zsyhServiceOrderService;

    @RequiresPermissions("zsyh:serviceOrderLog:view")
    @GetMapping()
    public String serviceOrderLog()
    {
        return prefix + "/serviceOrderLog";
    }

    /**
     * 查询订单过程追踪列表
     */
    /*@RequiresPermissions("zsyh:serviceOrderLog:list")*/
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhServiceOrderLog zsyhServiceOrderLog)
    {
        startPage();
        List<ZsyhServiceOrderLog> list = zsyhServiceOrderLogService.selectZsyhServiceOrderLogList(zsyhServiceOrderLog);
        return getDataTable(list);
    }

    /**
     * 查询订单过程追踪列表
     */
    /*@RequiresPermissions("zsyh:serviceOrderLog:list")*/
    @PostMapping("/listById")
    @ResponseBody
    public TableDataInfo listById(String id)
    {
        ZsyhServiceOrderLog zsyhServiceOrderLog = new ZsyhServiceOrderLog();
        zsyhServiceOrderLog.setOrderId(id);
        List<ZsyhServiceOrderLog> list = zsyhServiceOrderLogService.selectZsyhServiceOrderLogList(zsyhServiceOrderLog);
        return getDataTable(list);
    }

    /**
     * 导出订单过程追踪列表
     */
    @RequiresPermissions("zsyh:serviceOrderLog:export")
    @Log(title = "订单过程追踪", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhServiceOrderLog zsyhServiceOrderLog)
    {
        List<ZsyhServiceOrderLog> list = zsyhServiceOrderLogService.selectZsyhServiceOrderLogList(zsyhServiceOrderLog);
        ExcelUtil<ZsyhServiceOrderLog> util = new ExcelUtil<ZsyhServiceOrderLog>(ZsyhServiceOrderLog.class);
        return util.exportExcel(list, "订单过程追踪数据");
    }

    /**
     * 新增订单过程追踪
     */
    @GetMapping("/add/{orderId}")
    public String add(@PathVariable("orderId") String orderId, ModelMap mmap)
    {
        mmap.put("orderId", orderId);
        return prefix + "/add";
    }

    /**
     * 催单记录
     */
    @GetMapping("/add1/{orderId}")
    public String add1(@PathVariable("orderId") String orderId, ModelMap mmap)
    {
        mmap.put("orderId", orderId);
        return prefix + "/add1";
    }

    /**
     * 新增订单过程追踪
     */
    @GetMapping("/add2/{orderId}")
    public String add2(@PathVariable("orderId") String orderId, ModelMap mmap)
    {
        mmap.put("orderId", orderId);
        return prefix + "/add2";
    }

    /**
     * 新增保存订单过程追踪
     */
    /*@RequiresPermissions("zsyh:serviceOrderLog:add")*/
    @Log(title = "订单过程追踪", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhServiceOrderLog zsyhServiceOrderLog)
    {
        zsyhServiceOrderLog.setLogType("3");
        zsyhServiceOrderLog.setOperatorId(ShiroUtils.getSysUser().getUserName());
        return toAjax(zsyhServiceOrderLogService.insertZsyhServiceOrderLog(zsyhServiceOrderLog));
    }

    /**
     * 新增保存订单过程追踪
     */
    @Transactional
    /*@RequiresPermissions("zsyh:serviceOrderLog:add")*/
    @Log(title = "催单记录", businessType = BusinessType.INSERT)
    @PostMapping("/add1")
    @ResponseBody
    public AjaxResult addSave1(ZsyhServiceOrderLog zsyhServiceOrderLog)
    {
        zsyhServiceOrderLog.setLogType("3");
        zsyhServiceOrderLog.setOperatorId(ShiroUtils.getSysUser().getUserName());
        //修改订单状态
        int num = zsyhServiceOrderLogService.insertZsyhServiceOrderLog(zsyhServiceOrderLog);
        if(num >0){
            ZsyhServiceOrder zsyhServiceOrder = zsyhServiceOrderService.selectZsyhServiceOrderById(zsyhServiceOrderLog.getOrderId());
            zsyhServiceOrder.setOrderReminderState("2");
            zsyhServiceOrderService.updateZsyhServiceOrder(zsyhServiceOrder);
        }
        return toAjax(num);
    }

    /**
     * 新增保存订单过程追踪
     */
    @Transactional
    /*@RequiresPermissions("zsyh:serviceOrderLog:add")*/
    @Log(title = "平台审核添加日志", businessType = BusinessType.INSERT)
    @PostMapping("/add2")
    @ResponseBody
    public AjaxResult addSave2(ZsyhServiceOrderLog zsyhServiceOrderLog,String reviewResult)
    {
        ZsyhServiceOrder zsyhServiceOrder = zsyhServiceOrderService.selectZsyhServiceOrderById(zsyhServiceOrderLog.getOrderId());
        zsyhServiceOrderLog.setLogType("3");
        zsyhServiceOrderLog.setOperatorId(ShiroUtils.getSysUser().getUserName());
        if("1".equals(reviewResult)){
            //通过
            zsyhServiceOrder.setAuditPlatformTime(new Date());
            zsyhServiceOrder.setOrderState("202");
        }else{
            //驳回
            zsyhServiceOrder.setSettlementTime(null);
            zsyhServiceOrder.setOrderState("102");
        }
        zsyhServiceOrderService.updateZsyhServiceOrder(zsyhServiceOrder);
        return toAjax(zsyhServiceOrderLogService.insertZsyhServiceOrderLog(zsyhServiceOrderLog));
    }

    /**
     * 修改订单过程追踪
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhServiceOrderLog zsyhServiceOrderLog = zsyhServiceOrderLogService.selectZsyhServiceOrderLogById(id);
        mmap.put("zsyhServiceOrderLog", zsyhServiceOrderLog);
        return prefix + "/edit";
    }

    /**
     * 修改订单过程追踪
     */
    @GetMapping("/listLog/{orderId}")
    public String listLog(@PathVariable("orderId") String orderId, ModelMap mmap)
    {
        mmap.put("orderId", orderId);
        return prefix + "/serviceOrderLog";
    }

    /**
     * 修改保存订单过程追踪
     */
    /*@RequiresPermissions("zsyh:serviceOrderLog:edit")*/
    @Log(title = "订单过程追踪", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhServiceOrderLog zsyhServiceOrderLog)
    {
        if(!zsyhServiceOrderLog.getOperatorId().equals(ShiroUtils.getSysUser().getUserName())){
            return error("当前用户不是操作人员本人！");
        }
        return toAjax(zsyhServiceOrderLogService.updateZsyhServiceOrderLog(zsyhServiceOrderLog));
    }

    /**
     * 删除订单过程追踪
     */
    /*@RequiresPermissions("zsyh:serviceOrderLog:remove")*/
    @Log(title = "订单过程追踪", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        ZsyhServiceOrderLog zsyhServiceOrderLog = zsyhServiceOrderLogService.selectZsyhServiceOrderLogById(ids);
        if(!zsyhServiceOrderLog.getOperatorId().equals(ShiroUtils.getSysUser().getUserName())){
            return error("当前用户不是操作人员本人！");
        }
        return toAjax(zsyhServiceOrderLogService.deleteZsyhServiceOrderLogByIds(ids));
    }
}
